//
//  XS_ZhaopinDetailVC.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/20.
//

import UIKit
import WebKit

class XS_ZhaopinDetailVC: XS_BaseVC {
    var model: XS_queryRecruitmentListM!

    override func upView() {
//        let share = QMUIButton()
//        share.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
//        share.setBackgroundImage(UIImage(named: "icon_分享"), for: .normal)
//        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: share)
//        share.rx.tap.asDriver()
//            .drive(onNext: { [weak self] in
//                guard let `self` = self, let url = URL(string: "/rectruit?id=\(self.model.id)".spliceH5Api()) else { return }
//                let activityVc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
//                self.present(activityVc, animated: true)
//            }).disposed(by: disposeBag)
//        
        
        let button = XS_NoHighlightBtn()
        button.layer.cornerRadius = .scale*24
        button.layer.backgroundColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
        button.setTitle("立即报名", for: .normal)
        button.titleLabel?.font = .xsFont(name: .medium, .scale*16)
        view.addSubview(button)
        button.snp.makeConstraints({
            $0.height.equalTo(.scale*48)
            $0.bottom.equalTo(-.scale*31)
            $0.left.equalTo(.scale*16)
            $0.right.equalTo(-.scale*16)
        })
        
        button.rx.tap.asDriver()
            .drive(onNext: { [weak self] in
                guard let `self` = self, XS_Singleton.shared.infoModel != nil else {
                    XS_NVC.shared.toLogin(false)
                    return
                }
                let alert = XS_InfoBottomAlertVC()
                alert.actionSubject
                    .subscribe(onNext: { [weak self] type, completion in
                        guard let `self` = self, type == .sure else {
                            completion?(nil)
                            return
                        }
                        guard let name = alert.name.text, name.count > 0 else {
                            alert.view.xsShowMsg("请填写姓名！")
                            return
                        }
                        XS_NetWork.requestJson("/resume/deliveryResume", parameters: [
                            "recruitmentId":self.model.id,
                            "name": name,
                            "remark":alert.beizhu.text ?? ""
                        ]).subscribe(onNext: { [weak self] resp in
                            self?.view.xsShowMsg(resp.msg)
                            completion?(nil)
                        }).disposed(by: self.disposeBag)
                    }).disposed(by: self.disposeBag)
                self.present(alert, animated: false)
            }).disposed(by: disposeBag)
        
        let scroll = UIScrollView()
        scroll.bounces = false
        scroll.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        scroll.clipsToBounds = true
        scroll.contentInsetAdjustmentBehavior = .never
        view.addSubview(scroll)
        scroll.snp.makeConstraints({
            $0.left.right.top.equalToSuperview()
            $0.bottom.equalTo(button.snp.top).offset(-.scale*16)
        })
        
        let salaryRange = UILabel.xsLabel(size: .scale*16, fontName: .medium, color: #colorLiteral(red: 0.9294117647, green: 0.3960784314, blue: 0.2470588235, alpha: 1), alignment: .right, text: model.salaryRange)
        scroll.addSubview(salaryRange)
        salaryRange.snp.makeConstraints({
            $0.top.equalTo(.scale*16)
            $0.right.equalTo(scroll.snp.left).offset(.scale*359)
        })
        
        let positionName = UILabel.xsLabel(size: .scale*24, fontName: .medium, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), lines: 0, text: model.positionName)
        scroll.addSubview(positionName)
        positionName.snp.makeConstraints({
            $0.left.top.equalTo(.scale*16)
            $0.right.equalTo(salaryRange.snp.left).offset(-.scale*8)
        })
        
        let imgV1 = UIImageView(image: UIImage(named: "icon_12px_定位"))
        scroll.addSubview(imgV1)
        imgV1.snp.makeConstraints {
            $0.width.height.equalTo(.scale*12)
            $0.left.equalTo(.scale*16)
//            $0.top.equalTo(.scale*63)
            $0.top.equalTo(positionName.snp.bottom).offset(.scale*8)
        }
        let detailAddress = UILabel.xsLabel(size: .scale*12, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1), text: model.workPlace)
        scroll.addSubview(detailAddress)
        detailAddress.snp.makeConstraints({
            $0.centerY.equalTo(imgV1.snp.centerY)
            $0.left.equalTo(imgV1.snp.right).offset(.scale*4)
        })
        let imgV2 = UIImageView(image: UIImage(named: "icon_12px_日期"))
        scroll.addSubview(imgV2)
        imgV2.snp.makeConstraints {
            $0.width.height.equalTo(.scale*12)
            $0.left.equalTo(detailAddress.snp.right).offset(.scale*16)
            $0.top.equalTo(imgV1.snp.top)
        }
        let experienceRequirement = UILabel.xsLabel(size: .scale*12, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1), text: model.experienceRequirement)
        scroll.addSubview(experienceRequirement)
        experienceRequirement.snp.makeConstraints({
            $0.centerY.equalTo(imgV2.snp.centerY)
            $0.left.equalTo(imgV2.snp.right).offset(.scale*4)
        })
        let imgV3 = UIImageView(image: UIImage(named: "icon_12px_日期_1"))
        scroll.addSubview(imgV3)
        imgV3.snp.makeConstraints {
            $0.width.height.equalTo(.scale*12)
            $0.left.equalTo(experienceRequirement.snp.right).offset(.scale*16)
            $0.top.equalTo(imgV1.snp.top)
        }
        let minDegree = UILabel.xsLabel(size: .scale*12, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1), text: model.minDegree)
        scroll.addSubview(minDegree)
        minDegree.snp.makeConstraints({
            $0.centerY.equalTo(imgV3.snp.centerY)
            $0.left.equalTo(imgV3.snp.right).offset(.scale*4)
        })
        
        let top = UIView()
        top.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        scroll.addSubview(top)
        top.snp.makeConstraints({
//            $0.top.equalTo(.scale*94)
            $0.top.equalTo(imgV1.snp.bottom).offset(.scale*16)
            $0.height.equalTo(.scale*8)
            $0.left.width.equalToSuperview()
        })
        
        let label = UILabel.xsLabel(size: .scale*16, fontName: .medium, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), text: "职位详情")
        scroll.addSubview(label)
        label.snp.makeConstraints({
            $0.left.equalTo(.scale*16)
            $0.top.equalTo(top.snp.bottom).offset(.scale*17)
        })
        
        let (v1, lab1) = getTagView()
        scroll.addSubview(v1)
        v1.snp.makeConstraints({
            $0.height.equalTo(.scale*20)
            $0.top.equalTo(top.snp.bottom).offset(.scale*56)
            $0.left.equalTo(.scale*16)
        })
        let (v2, lab2) = getTagView()
        scroll.addSubview(v2)
        v2.snp.makeConstraints({
            $0.height.equalTo(.scale*20)
            $0.top.equalTo(top.snp.bottom).offset(.scale*56)
            $0.left.equalTo(v1.snp.right).offset(.scale*8)
        })
        lab1.text = model.experienceRequirement
        lab2.text = model.industryName
        
        let line = UIView()
        line.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        scroll.addSubview(line)
        line.snp.makeConstraints({
            $0.height.equalTo(1)
            $0.left.equalTo(.scale*16)
            $0.top.equalTo(top.snp.bottom).offset(.scale*92)
            $0.width.equalTo(.scale*343)
        })
        
        let detail = UILabel()
        detail.font = .xsFont(name: .regular, .scale*14)
        detail.numberOfLines = 0
        scroll.addSubview(detail)
        detail.snp.makeConstraints({
            $0.top.equalTo(line.snp.bottom).offset(.scale*16)
            $0.left.equalTo(.scale*16)
            $0.width.lessThanOrEqualTo(.scale*343)
//            $0.bottom.lessThanOrEqualToSuperview()
        })
        
        detail.frame = CGRect(x: 0, y: 200, width: .scale * 200, height: .scale * 800)
        print(self.model.positionDescription)
        DispatchQueue.global().async { [weak self] in
            guard let `self` = self else { return }
            if self.model.positionDescription.contains("><br><img") {
                self.model.positionDescription = self.model.positionDescription.replacingOccurrences(of: "><br><img", with: "><img")
            }
            
            let pWidth = .scale * 45
            if self.model.positionDescription.contains("max-width: 100%;") {
                self.model.positionDescription = self.model.positionDescription.replacingOccurrences(of: "max-width: 100%;", with: "max-width: \(pWidth)%;")
            }
            if self.model.positionDescription.contains("style=\"max-width:100%;") {
                self.model.positionDescription = self.model.positionDescription.replacingOccurrences(of: "style=\"max-width:100%;", with: "style=\"max-width:\(pWidth)%;")
            }
            if self.model.positionDescription.contains("style=\"max-width: 100%;") {
                self.model.positionDescription = self.model.positionDescription.replacingOccurrences(of: "style=\"max-width: 100%;", with: "style=\"max-width:\(pWidth)%;")
            } else {
                self.model.positionDescription = self.model.positionDescription.replacingOccurrences(of: "/><img", with: " style=\"max-width: \(pWidth)%;\"/><img")
            }

            
            if let data = self.model.positionDescription.data(using: .unicode),
               let text = try? NSMutableAttributedString(data: data, options: [.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil) {
                DispatchQueue.main.async {
                    detail.attributedText = text
                }
            }
        }
        
        let collections = XD_RecommendCollView()
        collections.recruitmentId = model.id
        collections.reLoadData()
        scroll.addSubview(collections)
        collections.snp.makeConstraints { (make) in
            make.top.equalTo(detail.snp.bottom)
            make.left.width.bottom.equalToSuperview()
            make.height.equalTo(.scale * 160)
        }
    }
    
    func getTagView() -> (UIView, UILabel) {
        let view = UIView()
        view.layer.cornerRadius = .scale*10
        view.layer.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        let label = UILabel.xsLabel(size: .scale*12, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1))
        view.addSubview(label)
        label.snp.makeConstraints({
            $0.center.equalToSuperview()
            $0.right.equalTo(-.scale*6)
        })
        return (view, label)
    }
}
